package com.lifesting.wfs.Runtime.addon;

import com.lifesting.wfs.Runtime.Context;
import com.lifesting.wfs.Runtime.ProcessInstance;
import com.lifesting.wfs.Runtime.RuntimeFactory;
import com.lifesting.wfs.Runtime.Token;
import com.lifesting.wfs.gd.ProcessDefinition;

public final class ExecuteService {
	private ProcessDefinition definition;
	private ProcessInstance instance;

	public ExecuteService(ProcessDefinition definition) {
		this.definition = definition;
	}

	public void start() throws WorkflowException {
		initInstance();
		instance.start();
	}

	private void initInstance() {
		if (instance == null) {
			instance = RuntimeFactory.eINSTANCE
					.createProcessInstance();
			instance.setDefinition(definition);
			instance.setExecutorFactory(new MinorExecutorFactoryImpl());
			Token token = RuntimeFactory.eINSTANCE.createToken();
			token.setExecutor(instance.getExecutorFactory().createExecutor(instance.getDefinition().getStart()));
			Context context = RuntimeFactory.eINSTANCE.createContext();
			token.setContext(context);
			instance.setRootToken(token);
			System.out.println(token.getProcessInstance());
		}
	}

}
